/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : app_freertos.c
  * Description        : FreeRTOS applicative file
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "app_freertos.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp_sht20.h"
#include "usart.h"
#include "string.h"
#include "cJSON.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 512 * 4
};
/* Definitions for myTask02 */
osThreadId_t myTask02Handle;
const osThreadAttr_t myTask02_attributes = {
  .name = "myTask02",
  .priority = (osPriority_t) osPriorityLow,
  .stack_size = 512 * 4
};
/* Definitions for myTask03 */
osThreadId_t myTask03Handle;
const osThreadAttr_t myTask03_attributes = {
  .name = "myTask03",
  .priority = (osPriority_t) osPriorityLow1,
  .stack_size = 512 * 4
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of myTask02 */
  myTask02Handle = osThreadNew(StartTask02, NULL, &myTask02_attributes);

  /* creation of myTask03 */
  myTask03Handle = osThreadNew(StartTask03, NULL, &myTask03_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}
/* USER CODE BEGIN Header_StartDefaultTask */
/**
* @brief Function implementing the defaultTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN defaultTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END defaultTask */
}

/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the myTask02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void *argument)
{
  /* USER CODE BEGIN myTask02 */
  /* Infinite loop */
  extern uint8_t  USART1_RxBuff[1024];
  extern volatile uint8_t USART1_RxCounter;	
  //BSP_SHT20_GetData();
  for(;;)
  {
	BSP_SHT20_GetData();
    osDelay(2000);
  }
  /* USER CODE END myTask02 */
}

/* USER CODE BEGIN Header_StartTask03 */
/**
* @brief Function implementing the myTask03 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask03 */
void StartTask03(void *argument)
{
  /* USER CODE BEGIN myTask03 */
  /* Infinite loop */
	extern uint8_t USART5_RxBuff[1024];
		extern uint8_t USART1_RxBuff[1024];
	extern volatile int flag;
	extern volatile int Tempup;
	extern volatile int Tempdown;
	extern volatile int Humiup;
	extern volatile int Humidown;
	char data[1024]={0};
  for(;;)
  {
	USART_printf(&huart1,USART5_RxBuff);	
	memcpy(data,USART5_RxBuff,1024);
		if(!strcmp(data,"BEEP_ON"))
		{
			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15,1);
			USART_printf(&huart1,"come in\n");	
		}
		else if(!strcmp(data,"BEEP_OFF"))
		{
			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15,0);
		}
		else if(!strcmp(data,"AUTO_ON"))
		{
			flag = 0;
		}
		else if(!strcmp(data,"AUTO_OFF"))
		{
			flag = 1;
		}
		else if(!strcmp(data,"HEAT_ON"))
		{
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);
		}
		else if(!strcmp(data,"HEAT_OFF"))
		{
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);
		}
		else if(!strcmp(data,"LIGHT_ON"))
		{
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,1);
		}
		else if(!strcmp(data,"LIGHT_OFF"))
		{
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,0);
		}
		else if(!strcmp(data,"FAN_ON"))
		{
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,1);
		}
		else if(!strcmp(data,"FAN_OFF"))
		{
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,0);
		}
		else if(strstr((char*)USART5_RxBuff,"Tempup") != NULL)
			{
				sscanf((char*)USART5_RxBuff,"Tempup %d",&Tempup);
				printf("Temup=%d\n",Tempup);
			}
		else if(strstr((char*)USART5_RxBuff,"Humiup") != NULL)
		{
			sscanf((char*)USART5_RxBuff,"Humiup %d",&Humiup);
		}
		
			memset(USART5_RxBuff,0,1024);

    osDelay(1000);
  }
  /* USER CODE END myTask03 */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

